<template>
	<div>
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<div>
				<el-form ref="catalogDialogFormRef" :rules="rules" :model="state.ruleForm" size="default" label-width="90px">
					<el-row>
						<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
							<el-form-item label="分类名称" prop="catalogName">
								<el-input v-model="state.ruleForm.catalogName" placeholder="请输入分类名称" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
							<el-form-item label="分类样式" prop="style">
								<el-select v-model="state.ruleForm.style" placeholder="选择商品分类展示样式" style="width: 100%">
									<el-option v-for="item in state.goodsCatalogStyles" :key="item.type" :label="item.title" :value="item.type" />
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
							<el-form-item label="分类描述" prop="description">
								<el-input v-model="state.ruleForm.description" type="textarea" placeholder="请输入分类描述" maxlength="150"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
			</div>
			<template #footer>
				<div class="dialog-footer">
					<el-button @click="handleCatalogDataClose" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit(catalogDialogFormRef)" size="default">{{ state.dialog.submitTxt }}</el-button>
				</div>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="goodsCatalogDialog">
import { reactive, ref, defineAsyncComponent, nextTick } from 'vue';
import { goodCatalogApi } from '/@/api/goods/catalog/index';
import { dictApi } from '/@/api/sys/dict';
import msg from '/@/utils/messageUtils';
import type { FormInstance, FormRules } from 'element-plus';
const catalogDialogFormRef = ref();
const emit = defineEmits(['refresh']);
const state = reactive({
	dialog: {
		isShowDialog: false,
		type: '',
		title: '根分类',
		submitTxt: '更新',
	},
	catalogData: [],
	ruleForm: {
		catalogName: '',
		id: '0',
		parentId: '-1',
		description: '',
		style: '',
	},
	catelogImageType: 0,

	goodsCatalogStyles: [],
});

const rules = reactive<FormRules>({
	catalogName: [{ required: true, message: '分类名称不能为空', trigger: 'blur' }],
	parentId: [{ required: true, message: '父分类不能为空', trigger: 'blur' }],
	style: [{ required: true, message: '分类样式不能为空', trigger: 'blur' }],
});

const loadStyle = async () => {
	let { data } = await dictApi().queryByCategory('goods_catalog_style');
	state.goodsCatalogStyles = data;
};

const onOpenDialog = () => {
	loadStyle();
	state.dialog.isShowDialog = true;
	onLoadGoodsCatalogDetail('0');
};

const onLoadGoodsCatalogDetail = async (id: string) => {
	try {
		const { data } = await goodCatalogApi().view(id);
		state.ruleForm.catalogName = data.catalogName;
		state.ruleForm.description = data.description;
		state.ruleForm.style = data.style;
		console.log('data: ', data);
	} catch (error) {
		msg().error('获取分类详情失败');
	}
};

const handleCatalogDataClose = () => {
	state.dialog.isShowDialog = false;
};
const onSubmit = async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	if ('object' == typeof state.ruleForm.parentId) {
		state.ruleForm.parentId = state.ruleForm.parentId[0];
	}

	await formEl.validate(async (valid) => {
		if (valid) {
			try {
				await goodCatalogApi().updateRoot(state.ruleForm);
				msg().success('success');
				closeDialog(); // 关闭弹窗
				emit('refresh');
			} catch (error: any) {
				msg().error('error');
			}
		}
	});
};

const choiceFileImage = (type: number) => {
	state.catelogImageType = type;

	/**
	 * type = 0 表示分类图标
	 * type = 1 表示分类图片
	 *
	 */

	FileChoiceRef.value.openFileChoideDialog();
};

const choiceFile = (choiceFileInfo: ChoiceFileInfo) => {
	/**
	 * type = 0 表示分类图标
	 * type = 1 表示分类图片
	 *
	 */
	console.log('choiceFileInfo: ', JSON.stringify(choiceFileInfo));
	if (state.catelogImageType === 0) {
		state.ruleForm.showIconUrl = choiceFileInfo.visitAbsPath;
		state.ruleForm.iconUrl = choiceFileInfo.visitRelativePath;
		state.ruleForm.iconFileId = choiceFileInfo.id;
	} else {
		state.ruleForm.showPicUrl = choiceFileInfo.visitAbsPath;
		state.ruleForm.picUrl = choiceFileInfo.visitRelativePath;
		state.ruleForm.picFileId = choiceFileInfo.id;
	}
};

const closeDialog = () => {
	state.dialog.isShowDialog = false;
};

defineExpose({
	onOpenDialog,
});
</script>

<style scoped lang="scss">
.goods-image {
	display: flex;
	align-items: center;
	justify-content: center;
	height: 50px;
	width: 50px;
	cursor: pointer;
	border: 1px dashed var(--el-border-color-darker);
	&:hover {
		border: 1px dashed var(--el-color-primary);
	}
}
</style>
